package cn.hn3l.agriculture.controllers.manager.goods;

import cn.hn3l.agriculture.entitis.behoof.Crop;
import cn.hn3l.agriculture.entitis.behoof.Sympton;
import cn.hn3l.agriculture.entitis.form.GoodsForm;
import cn.hn3l.agriculture.entitis.goods.Goods;
import cn.hn3l.agriculture.entitis.pictures.Picture;
import cn.hn3l.agriculture.services.interfaces.FileService;
import cn.hn3l.agriculture.services.interfaces.goods.GoodsService;
import cn.leve.web.utils.servlet.HttpRequestUtils;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Wang Weiwei
 * @Since 16-10-17
 * @Describe
 */
@Controller
@RequestMapping("/manager/goods")
public class ManagerGoodsController {

    @Resource
    private GoodsService goodsService;

    @Resource
    private FileService fileService;
    /**商品列表界面*/
    @RequestMapping("/show_goods/{page}")
    public String showAllGoods(@PathVariable int page, Model model){
        List<Goods> goodses = goodsService.getPageWithGoods(page);
        Map<String,String> map = new HashMap<>();
        map.put("title","商品管理列表-第" + page + "页");
        model.addAttribute("web",map);
        model.addAttribute("list",goodses);
        return "/manager/goods/goods_list_content";
    }

    /**修改商品页面视图*/
    @RequestMapping(value = "/editor_goods/{id}")
    public String editorGoods(@PathVariable int id,Model model){
        Map<String,String> map = new HashMap<>();
        map.put("title","修改" );
        model.addAttribute("web",map);
        return "manager/goods/editor_goods";
    }

    /**新增商品*/
    @RequestMapping(value = "/add_goods")
    public String addGoods(Model model){
        Map<String,String> map = new HashMap<>();
        map.put("title","新增商品" );
        model.addAttribute("web",map);
        return "manager/goods/add_goods";
    }
    /*produces = "application/json;charset=UTF-8"*/

    @RequestMapping(value = "/push_new_goods",
                    method = RequestMethod.POST
                    )
    public void pushNewGoods(@RequestBody GoodsForm form,
                             Writer writer) throws Exception{
        List<Picture> goodsPictureList = fileService.parseHTMLToPictureList(form.getPictureHtml());
        form.getGoods().setPictures(goodsPictureList);
        String result = null;
        try{
            result = goodsService.saveGoodsAndCropsAndSmptonsAndPicture(form.getGoods(),form.getCrops(),form.getSymptons());
        }catch (Exception e){
            e.printStackTrace();
            result =e.getMessage();
        }
        writer.write(result);
        System.out.println(JSON.toJSON(form.getGoods()));
    }


    /**
     * @ModelAttribute一个具有如下三个作用：
    ①绑定请求参数到命令对象：放在功能处理方法的入参上时，
    用于将多个请求参数绑定到一个命令对象，从而简化绑定流程，
    而且自动暴露为模型数据用于视图页面展示时使用；
    ②暴露表单引用对象为模型数据：放在处理器的一般方法（非功能处理方法）上时，
    是为表单准备要展示的表单引用对象，如注册时需要选择的所在城市等，
    而且在执行功能处理方法（@RequestMapping注解的方法）之前，
    自动添加到模型对象中，用于视图页面展示时使用；
    ③暴露@RequestMapping方法返回值为模型数据：
    放在功能处理方法的返回值上时，是暴露功能处理方法的返回值为模型数据，
    用于视图页面展示时使用。
     @SessionAttribute
     */

    @ModelAttribute("statisticsList")
    public List<Map> bindStatisticsList(HttpServletRequest request){
        String href = "/manager/goods/statistics";
        Map<String,String> stringMap = new HashMap<>();
        stringMap.put("title","商品数目分析(商品种类,适用作物,适用症状商品数目分析)");
        stringMap.put("url", HttpRequestUtils.basePath(request)+ href + "/goods_count");
        stringMap.put("requestCount","100");
        List<Map> mapsList = new ArrayList<>();
        mapsList.add(stringMap);
        return mapsList;
    }

    /**商品信息统计列表*/
    @RequestMapping(value = "/statistics/list")
    public String statisticsList(@ModelAttribute("statisticsList") List<Map> statisticsList,
                                 HttpServletRequest request) throws IOException {
        return "manager/goods/goods_statistics_list";
    }


    /***/
    @RequestMapping(value = "/statistics/goods_count")
    public String statisticsList(Model model){

        return "manager/goods/goods_statistics_goods_count";
    }
}
